package com.business.web.spring;


import java.util.List;
import org.apache.log4j.Logger;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//import com.workflow.WorkFlowUtils;
import com.common.db.JsonVO;
import com.common.db.Pg;
import com.common.utils.BeanUtils;
import com.business.domain.T_task_node_instance;
//import com.business.mapper.T_task_node_instanceMapper;
import com.business.service.T_task_node_instanceService;

@Controller
@RequestMapping("/t_task_node_instance")

public class T_task_node_instanceController
{
	private static Logger log = Logger.getLogger(T_task_node_instanceController.class.getName());

	@Resource
    private T_task_node_instanceService t_task_node_instanceService;

	//@Autowired
	//private WorkFlowUtils workFlowUtils;

	//@Resource
	//private T_task_node_instanceMapper mapper;
	//private int id;
	//private int[] ids;
	private Pg pg;

	/**
	 * 1、获取 T_task_node_instance信息列表，并且在列表页面展示
	 * @param request
	 * @return Pg，json格式
	 */
	@RequestMapping("/getAllT_task_node_instance")
	@ResponseBody
	public Pg getAllT_task_node_instance(HttpServletRequest request){
        int pageSize = 10;
        int startIndex = 0;
        T_task_node_instance t_task_node_instance = new T_task_node_instance();
        try{
	        if(request.getParameter("pageSize")!=null){
	        	pageSize = BeanUtils.stringToInt("pageSizeFlag", request.getParameter("pageSize"));
	        }
	        if(request.getParameter("startIndex")!=null){
	            startIndex = BeanUtils.stringToInt("startIndexFlag", request.getParameter("startIndex"));
	        }
			if(request.getParameter("biztype")!=null&&!"".equals(request.getParameter("biztype"))){ //取得动作流 流程英文名称，后台查询数据关联使用
				t_task_node_instance.setBiztype(request.getParameter("biztype"));
			}
	        if(request.getParameter("process_code")!=null){ //取得动作流 流程英文名称，后台查询数据关联使用
	        	t_task_node_instance.setProcess_code(request.getParameter("process_code"));
	        }
			if(request.getParameter("tninstancestatus")!=null&&!"".equals(request.getParameter("tninstancestatus"))){
	        	t_task_node_instance.setTninstancestatus(request.getParameter("tninstancestatus"));
	        }
			if(request.getParameter("creater")!=null&&!"".equals(request.getParameter("creater"))){
	        	t_task_node_instance.setCreater(request.getParameter("creater"));
	        }
			if(request.getParameter("createtime")!=null&&!"".equals(request.getParameter("createtime"))){
	        	t_task_node_instance.setCreatetime(request.getParameter("createtime"));
	        }
			if(request.getParameter("updater")!=null&&!"".equals(request.getParameter("updater"))){
	        	t_task_node_instance.setUpdater(request.getParameter("updater"));
	        }
			if(request.getParameter("updatetime")!=null&&!"".equals(request.getParameter("updatetime"))){
	        	t_task_node_instance.setUpdatetime(request.getParameter("updatetime"));
	        }
			if(request.getParameter("firststep")!=null&&!"".equals(request.getParameter("firststep"))){
	        	t_task_node_instance.setFirststep(request.getParameter("firststep"));
	        }
			if(request.getParameter("laststep")!=null&&!"".equals(request.getParameter("laststep"))){
	        	t_task_node_instance.setLaststep(request.getParameter("laststep"));
	        }
			if(request.getParameter("note")!=null&&!"".equals(request.getParameter("note"))){
	        	t_task_node_instance.setNote(request.getParameter("note"));
	        }
			if(request.getParameter("userid")!=null&&!"".equals(request.getParameter("userid"))){
	        	t_task_node_instance.setUserid(request.getParameter("userid"));
	        }
			if(request.getParameter("roleid")!=null&&!"".equals(request.getParameter("roleid"))){
	        	t_task_node_instance.setRoleid(request.getParameter("roleid"));
	        }
      	    if(request.getParameter("tninstanceid")!=null&&!"".equals(request.getParameter("tninstanceid"))){
	        	t_task_node_instance.setTninstanceid(new Integer(request.getParameter("tninstanceid")));
	        }
			if(request.getParameter("instanceid")!=null&&!"".equals(request.getParameter("instanceid"))){
				t_task_node_instance.setInstanceid(new Integer(request.getParameter("instanceid")));
			}
			if(request.getParameter("appid")!=null&&!"".equals(request.getParameter("appid"))){
				t_task_node_instance.setAppid(new Integer(request.getParameter("appid")));
			}
	        if(request.getParameter("startdate")!=null&&!"".equals(request.getParameter("startdate"))){
	        	t_task_node_instance.setStartdate(request.getParameter("startdate"));
	        }
	        if(request.getParameter("enddate")!=null&&!"".equals(request.getParameter("enddate"))){
	        	t_task_node_instance.setEnddate(request.getParameter("enddate"));
	        }
	        t_task_node_instance.setStartNo( startIndex );
	        t_task_node_instance.setPageSize( pageSize );
	        t_task_node_instance.setEndNo( pageSize );
//	    	List<T_task_node_instance> findAll = t_task_node_instanceService.findAll(t_task_node_instance);
//			int totalCount = t_task_node_instanceService.findCountAll(t_task_node_instance);
			List<T_task_node_instance> findAll = t_task_node_instanceService.findAllByGroup(t_task_node_instance);
			int totalCount = t_task_node_instanceService.findCountAllByGroup(t_task_node_instance);
	       	pg = new Pg(findAll, totalCount, pageSize, startIndex);
	       	request.setAttribute("t_task_node_instanceList", findAll);
		    request.setAttribute("pg", pg);
	    } catch (Exception e) {
			e.printStackTrace();
			log.error("------query t_task_node_instanceList error:"+e);
		}

		return pg;
	}

	/**
	 * 2、新增保存  T_task_node_instance信息并且返回成功或者失败信息json格式结果如：result:[success/error]
	 * @param t_task_node_instance
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveT_task_node_instance")
	@ResponseBody
	public JsonVO saveT_task_node_instance(T_task_node_instance t_task_node_instance, HttpServletRequest request,HttpServletResponse response){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		//如果有特殊需求，需要将json对象值转成java实体对象属性值
		if(t_task_node_instanceService.save(t_task_node_instance)>0){
	        vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 3、修改T_task_node_instance信息并且返回成功或者失败信息json格式结果如：result:[success/error]
	 * @param t_task_node_instance
	 * @param request
	 * @return JsonVO
	 */
	@RequestMapping("/updateT_task_node_instance")
	@ResponseBody
	public JsonVO updateT_task_node_instance(T_task_node_instance t_task_node_instance, HttpServletRequest request){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		//如果有特殊值需要处理，在此处手工转换
		//T_task_node_instance t_task_node_instanceTemp = t_task_node_instanceService.findById(t_task_node_instance);
		//t_task_node_instanceTemp.setXXX(t_task_node_instance.getXXX());
		//if(t_task_node_instanceService.update(t_task_node_instanceTemp)){

		if(t_task_node_instanceService.update(t_task_node_instance)>0){
	        vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 设置流程单个审核节点对应审核人为新的审核人， 即：转角人；
	 * @param t_task_node_instance
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateT_task_node_instanceSHR")
	@ResponseBody
	public JsonVO updateT_task_node_instanceSHR(T_task_node_instance t_task_node_instance, HttpServletRequest request){
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		String new_userid = t_task_node_instance.getUserid();
		t_task_node_instance = t_task_node_instanceService.findById(t_task_node_instance);
		//设置新的userid到对象中；即转角人
		t_task_node_instance.setUserid(new_userid);
		if(t_task_node_instanceService.update(t_task_node_instance)>0){
			vo.setResult("success");
			vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 4、根据id查询T_task_node_instance单个信息并显示在修改页面
	 * @param id
	 * @param request
	 * @return T_task_node_instance json格式字符串
	 */
	@RequestMapping("/getT_task_node_instance")
	@ResponseBody
	public T_task_node_instance getT_task_node_instance(Integer id,HttpServletRequest request){
		T_task_node_instance t_task_node_instance = new T_task_node_instance();
		t_task_node_instance.setTninstanceid(id);
		return t_task_node_instanceService.findById(t_task_node_instance);
	}

	/**
	 * 5、删除T_task_node_instance根据id
	 * @param id
	 * @param request
	 * @param response
	 * return JsonVO ：json格式字符串
	 */
	@RequestMapping("/delT_task_node_instance")
	@ResponseBody
	public JsonVO delT_task_node_instance(Integer id,HttpServletRequest request,HttpServletResponse response){
        JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);

		T_task_node_instance t_task_node_instance = new T_task_node_instance();
		t_task_node_instance.setTninstanceid(id);

		if(t_task_node_instanceService.delete(t_task_node_instance)>0){
			vo.setResult("success");
	        vo.setJieguo(true);
		}
		return vo;
	}

	/**
	 * 6、跳转到t_task_node_instance/toT_task_node_instanceList查询所有数据页面
	 *  return /WEB-INF/views/business/t_task_node_instance/t_task_node_instanceList.jsp
	 */
    @RequestMapping(value = "/toT_task_node_instanceList")
    public String toT_task_node_instanceList() {
    	log.debug("------跳转到/WEB-INF/views/business/t_task_node_instance/t_task_node_instanceList.jsp------");
        return "business/t_task_node_instance/t_task_node_instanceList";
    }

	/**
	 * 7、新增、修改、详情页面t_task_node_instance/toT_task_node_instanceCurd跳转
     * business/t_task_node_instance/T_task_node_instanceCurd.jsp
	 */
    @RequestMapping(value = "/toT_task_node_instanceCurd")
    public String toT_task_node_instanceCurd() {
    	log.debug("------跳转到/WEB-INF/views/business/t_task_node_instance/t_task_node_instanceCurd.jsp------");
        return "business/t_task_node_instance/t_task_node_instanceCurd";
    }

	/**
	 * 7、新增、修改、详情页面t_task_node_instance/toT_task_node_instanceCurd跳转
	 * business/t_task_node_instance/T_task_node_instanceCurd.jsp
	 */
	@RequestMapping(value = "/toT_task_node_instanceCurdSHR")
	public String toT_task_node_instanceCurdSHR() {
		log.debug("------跳转到/WEB-INF/views/business/t_task_node_instance/t_task_node_instanceCurdSHR.jsp------");
		return "business/t_task_node_instance/t_task_node_instanceCurdSHR";
	}

	/**
     * 8、批量删除T_task_node_instance根据主键id
     * 是否要实现批量删除，待考虑，基本上用途不大
     * @param id
     * @param request
     * @param response
    @RequestMapping("/delT_task_node_instanceBatch")
    public String delT_task_node_instanceBatch(HttpServletRequest request,HttpServletResponse response){
        String[] ids = request.getParameterValues("ids");
        System.out.println("------ids="+ids);
        T_task_node_instance t_task_node_instance = new T_task_node_instance();
        int j=0;
        for(int i =0;i< ids.length;i++){
            j++;
            log.error("------ids[i]="+ids[i]);
            t_task_node_instance.setTninstanceid(new Integer(ids[i]).intValue());
            t_task_node_instanceService.delete(t_task_node_instance);
        }
        if(j==ids.length){
            log.debug("------t_task_node_instance批量删除成功！------");
        }else{
            log.error("------t_task_node_instance批量删除部分成功！部分失败！------");
        }

        return "redirect:/t_task_node_instance/getAllT_task_node_instance";
    }
*/



}









